var pages = 1;
var tpnum = 0;
$(function() {
	if(getCookieValue("token")==""){
		window.location.href = 'l-userlogin.html'
	}
	
	
//	$('.phoneuser').click(function(){
//			alert(22)
//		})
	buyOrder({
		"pages": 1,
		"userUuid":5
	}, function(data) {
		$('.mui-table-view').html(template('buyList', data));

		//点击删除
		myOrderDelete();
		//上拉加载
		orderUp();
		//电话和微信监控
		phoneWc();
		//点击跳转
		orderClick();
	});

	//排序点击
	$('.ld_order a').on('tap', function() {
		/*当前点击的A*/
		var $this = $(this);
		/*如果之前没有选择*/
		if(!$this.hasClass('now')) {
			/*选中，其他的不选中，箭头默认朝下*/
			$this.addClass('now').siblings().removeClass('now')
				.find('span').removeClass('fa-angle-up').addClass('fa-angle-down');
		}
		/*有now的时候*/
		else {
			/*改当前的箭头方向*/
			if($this.find('span').hasClass('fa-angle-down')) {
				$this.find('span').removeClass('fa-angle-down').addClass('fa-angle-up');
			} else {
				$this.find('span').removeClass('fa-angle-up').addClass('fa-angle-down');
			}
		}
	});

	//价格排序
	$('#price-order').on('tap', function() {	
		mui('#mui-scroll-wrapper').pullRefresh().refresh(true);
		mui('#mui-scroll-wrapper').scroll().setTranslate(0,0);
		if($(this).hasClass('now') && $(this).children().hasClass('fa-angle-down')) {
			buyOrder({
				"pages": 1,
				"sort": 2,
				"userUuid":CD.geturl().business
			}, function(data) {
				$('.mui-table-view').html(template('buyList', data));
				//点击删除
				myOrderDelete();
				tpnum = 2;
				pages = 1;
			
			});
		} else {
			buyOrder({
				"pages": 1,
				"sort": 3,
				"userUuid":CD.geturl().business
			}, function(data) {
				$('.mui-table-view').html(template('buyList', data));
				//点击删除
				myOrderDelete();
				tpnum = 3;
				pages = 1;
				
			});
		}
	});

	//时间排序
	$('#time-order').on('tap', function() {		
		mui('#mui-scroll-wrapper').pullRefresh().refresh(true);
		mui('#mui-scroll-wrapper').scroll().setTranslate(0,0);
		if($(this).hasClass('now') && $(this).children().hasClass('fa-angle-down')) {
			buyOrder({
				"pages": 1,
				"sort": 0,
				"userUuid":CD.geturl().business
			}, function(data) {				
				$('.mui-table-view').html(template('buyList', data));
				tpnum = 0;
				pages = 1;			
			});
		} else {
			buyOrder({
				"pages": 1,
				"sort": 1,
				"userUuid":CD.geturl().business
			}, function(data) {
				$('.mui-table-view').html(template('buyList', data));
				tpnum = 1;
				pages = 1;				
			});
		}
		
	});
	orderUp(pages);

})

//预约遍历
var buyOrder = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/goods/findCollectClientBayUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}

//点击跳转
var orderClick = function() {
	$('.headImg').on('tap', function() {
		var productId = $(this).attr('data-id');
		var userUuid = $(this).attr('data-userid');
		window.location.href = 'v-shop-product.html?id=' + productId+'&business='+userUuid;
	});
}

//上拉加载
var orderUp = function() {
	mui.init({
		pullRefresh: {
			container: '#mui-scroll-wrapper',
			up: {
				callback: function() {
					var that = this;
					console.log(1);
					pages++;
					buyOrder({
						"pages": pages,
						"sort": tpnum,
						"userUuid":CD.geturl().business
					}, function(data) {
						$('.mui-table-view').append(template('buyList', data));
						//点击删除
						myOrderDelete();
						//点击监控
						orderClick();
						//电话和微信监控
						phoneWc();
						if(data.rows.length) {
							that.endPullupToRefresh();
						} else {
							that.endPullupToRefresh(true);
						}
					})
				}
			}
		}
	});
}

var phoneWc = function() {
	$('.mybuyer-open').on('tap', function() {
		var imgUrl = $(this).attr('data-urll');
		console.log(imgUrl);
		$('.mybuyer-wecht-show').css({
			"display": "block"
		});
		//更换图片链接
		$('#mybuyer-change img').attr({
			"src": imgUrl
		});
		return false;
	});
	$('#mybuyer-close').on('tap', function() {
		$('.mybuyer-wecht-show').css({
			"display": "none"
		});
	});
	
	$('.mybuyer-phone').on('tap',function(){
		var thisPhone = $(this).attr('data-phone');
		console.log(thisPhone)
		$('.mybuyer-phone-show').stop().fadeIn();
		$('.mybuyer-phone-show').children().children('span').html(thisPhone);
	});
	$('.mybuyer-phone-show').click(function(){
		$('.mybuyer-phone-show').stop().fadeOut();
	});
}

//点击删除接口
var orderDelete = function(goodsId, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/order/deleteClient",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			"goodsId": goodsId,
			"userUuid":CD.geturl().business
		},
		dataType: "json",
		success: function(data) {
			console.log(data, 222);
			callback && callback(data);
		}
	})
}

//删除预约
var myOrderDelete = function(){
	$('.myOrder-delete').on('tap', function() {
			var goodsId = $(this).attr('data-id');
			var thisNum = $(this).parent().parent('.mui-table-view-cell');
			console.log(goodsId);
			$('.buy-delete').stop().fadeIn();
			$('#buy-delete-close').on('tap', function() {
				$('.buy-delete').stop().fadeOut();
			});
			$('#buy-delete-open').on('tap', function() {
				$('.buy-delete').stop().fadeOut();
				thisNum.stop().fadeOut(500);
				//删除
				orderDelete(goodsId);
			});
		});
}
